package hua_rong_dao;


import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;

//
public class Hua_Rong_Road extends JFrame implements ActionListener, MouseListener, KeyListener {
    Person person[] = new Person[10];
    JButton left, right, above, below;
    JButton restart = new JButton("重新开始");//添加重新开始按钮

    public Hua_Rong_Road() {//方法设置按钮
        init();//设置按钮
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(800, 250, 320, 500);//组件在容器上的起点和长宽
        setVisible(true);//向控件中添加元素
        validate();//刷新
    }

    public void init() {
        setLayout(null);//设置布局默认
        add(restart);//添加重新开始
        restart.setBounds(100, 320, 120, 35);//按钮大小
        restart.addActionListener(this);//注册监听器
        String name[] = { "曹操", "关羽", "张", "刘", "周", "黄", "兵", "兵", "兵", "兵" };
        for (int k = 0; k < name.length; k++) {
            person[k] = new Person(k, name[k]);
            person[k].addMouseListener(this);//每个按钮都注册鼠标事件监听
            person[k].addKeyListener(this);//注册键盘事件监听
            add(person[k]);
        }
        person[0].setBounds(104, 54, 100, 100);
        person[1].setBounds(104, 154, 100, 50);
        person[2].setBounds(54, 154, 50, 100);
        person[3].setBounds(204, 154, 50, 100);
        person[4].setBounds(54, 54, 50, 100);
        person[5].setBounds(204, 54, 50, 100);
        person[6].setBounds(54, 254, 50, 50);
        person[7].setBounds(204, 254, 50, 50);
        person[8].setBounds(104, 204, 50, 50);
        person[9].setBounds(154, 204, 50, 50);
        person[9].requestFocus();
        left = new JButton();// 四个边界
        right = new JButton();
        above = new JButton();
        below = new JButton();
        add(left);
        add(right);
        add(above);
        add(below);
        left.setBounds(49, 49, 5, 260);
        right.setBounds(254, 49, 5, 260);
        above.setBounds(49, 49, 210, 5);
        below.setBounds(49, 304, 210, 5);
        validate();
    }

    public void keyPressed(KeyEvent e) {
        Person man = (Person) e.getSource();
        if (e.getKeyCode() == KeyEvent.VK_DOWN)
            go(man, below);
        if (e.getKeyCode() == KeyEvent.VK_UP)
            go(man, above);
        if (e.getKeyCode() == KeyEvent.VK_LEFT)
            go(man, left);
        if (e.getKeyCode() == KeyEvent.VK_RIGHT)
            go(man, right);
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        Person man = (Person) e.getSource();
        int x = -1, y = -1;
        x = e.getX();
        y = e.getY();
        int w = man.getBounds().width;
        int h = man.getBounds().height;
        if (y > h / 2)
            go(man, below);
        if (y < h / 2)
            go(man, above);
        if (x < w / 2)
            go(man, left);
        if (x > w / 2)
            go(man, right);
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void go(Person man, JButton direction) {
        boolean move = true;
        Rectangle manRect = man.getBounds();
        int x = man.getBounds().x;
        int y = man.getBounds().y;
        if (direction == below)
            y = y + 50;
        else if (direction == above)
            y = y - 50;
        else if (direction == left)
            x = x - 50;
        else if (direction == right)
            x = x + 50;
        manRect.setLocation(x, y);
        Rectangle dirctionRect = direction.getBounds();
        for (int k = 0; k < 10; k++) {
            Rectangle personRect = person[k].getBounds();
            if ((manRect.intersects(personRect)) && (man.number != k))
                move = false;
        }
        if (manRect.intersects(dirctionRect))
            move = false;
        if (move == true)
            man.setLocation(x, y);
    }

    public void actionPerformed(ActionEvent e) {
        dispose();
        new Hua_Rong_Road();
    }
}